package com.system.log;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.itogis.rest.dao.LoginDao;
import com.system.exception.ApplicationException;
@Component
@Aspect
@Transactional(propagation = Propagation.REQUIRES_NEW)
public class VnetLoggingAspect {
	private final Log logger = LogFactory.getLog(this.getClass());
	
	@SuppressWarnings("unused")
	@Autowired
	private LoginDao loginDao;

	@Around("execution (* com.itogis.rest.serivce..*Impl.*(..))")	
	@Transactional(noRollbackFor = ApplicationException.class)
	public Object logService(ProceedingJoinPoint pjp) throws Throwable {
		LogCoreImpl mel = new LogCoreImpl();
		try {
			mel.startLogging(pjp);
			Object returnValue = pjp.proceed();
			mel.logReturn(returnValue);
			mel.setResult(0L);
			return returnValue;
		} catch (ApplicationException ae) {
			mel.logThrowable(ae);
			mel.setResult(Long.valueOf(ae.getErrorCode()));
			mel.setReturnValue(ae.getMessage());
			throw ae;
		} catch (Throwable t) {
			t.printStackTrace();
			mel.logThrowable(t);
			mel.setResult(-1L);
			ApplicationException ae = new ApplicationException(t);
			throw ae;
		} finally {
			mel.completeLogging(this.logger);
//			SystemArgument systemArgument = this.systemArgumentDao
//					.load(SystemArgument.Key.VNET_CORE_LOG_TYPE);
//			// ALL,EXCEPTION,NONE
//			if ((systemArgument.getValue().equals("EXCEPTION") && mel
//					.getResult() != 0L)
//					|| (systemArgument.getValue().equals("ALL"))) {
//				logCoreImplDao.save(mel);
//			}
			loginDao.getVoteEventById(1L);
		}
	}
}
